package net.ecpt.clm.rest;

import static io.restassured.RestAssured.given;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import javolution.util.FastList;

public class RestHelper {

    public static final String appId = "15000850008";
    public static final String appKey = "Sd6qkHm9o4LaVluYRX5pUFyNuiu2a8oi";
    
    public static void setUP() {
	// 指定 URL 和端口号
	//RestAssured.baseURI = "http://127.0.0.1";
	RestAssured.baseURI = "http://eim.kejicloud.com/";
	RestAssured.port = 80;
	RestAssured.basePath = "/sysServer/online/rest/";
    }
    
    public static String sendGET(String url,Map<String,Object> params){
	setUP();
	//加入签名
	params.put("appId", appId);
	String sign = getValidateSign(params);
	params.put("sign", sign);
	Response response = given().header("accept", "application/json").params(params).get(url);
	String result = response.print();
	return result;
    } 
    
    
    public static String sendPOST(String url,Map<String,Object> params){
	/**
	 * POST乱码. 目前不知道原因
	 */
	setUP();
	//加入签名
	params.put("appId", appId);
	String sign = getValidateSign(params);
	params.put("sign", sign);
	Response response = given().params(params).get(url);
	String result = response.print();
	return result;
    }
    
    /**
     * 生成签名
     */
    public static String getValidateSign(Map<String,Object> param) {
	String validateSign = PayRaneUtil.createSign(param, appKey);
	System.out.println(validateSign);
	return validateSign;
    }
    public static void main(String[] args) {
	String str = "[{\"scomment\":\"测试摘要01\",\"recaccount\":\"4062540688526658\",\"prepay\":\"0\",\"money\":10000,\"rate\":1,\"inoutbusiclass\":\"收支项目\",\"balatype\":\"银行转账\",\"objtype\":\"0\",\"dept\":\"\",\"psndoc\":\"\",\"currtype\":\"CNY\",\"customer\":\"yangpeng01\"},{\"scomment\":\"测试摘要01\",\"recaccount\":\"4062540688526658\",\"prepay\":\"0\",\"money\":10000,\"rate\":1,\"inoutbusiclass\":\"收支项目\",\"balatype\":\"银行转账\",\"objtype\":\"1\",\"dept\":\"1111\",\"psndoc\":\"\",\"currtype\":\"CNY\",\"customer\":\"\"}]"; 
	List<Map> s = analysisJson(str);
	System.out.println(s);
    }
    @SuppressWarnings("rawtypes")
    public static List<Map> analysisJson(String str) {
	List<Map> list = FastList.newInstance();
	list = JSONObject.parseArray(str, Map.class);
	return list;
    }
}
